﻿using Azure.Core.Pipeline;
using LPLS.Commons.Enums;
using LPLS.DBEntities.ConfigModels.DataCollectionConfigModule.DataCollectionType.PLC;
using LPLS.DBInterface.ConfigModels.DataCollectionConfigModule.PLC;
using Prism.Commands;
using Prism.Mvvm;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;

namespace DataCollectionVarConfig.ViewModels
{
    public class DataCollectionPLCVarConfigViewModel : BindableBase
    {

        public DataCollectionPLCVarConfigViewModel(IPLCVarPropertyConfigService pLCVarPropertyConfigService)
        {
            _ipLCVarPropertyConfigService = pLCVarPropertyConfigService;
           SelectModel = new PLCVarPropertyConfig();
            LoadDataTypes();
            LoadItemList();
            DataCollectionVarConfigEvent.DataCollectionDeviceChangeEvent += DataCollectionVarConfigEvent_DataCollectionDeviceChangeEvent;
            AddCommand = new DelegateCommand(AddData);
            UpdateCommand= new DelegateCommand(UpdateData); 
        }


        private DataCollectionVarConfigEventModel eventModel { get; set; }
        private void DataCollectionVarConfigEvent_DataCollectionDeviceChangeEvent(DataCollectionVarConfigEventModel obj)
        {
            Message = "";

            eventModel = obj;

            LoadItemList();
        }

        private readonly IPLCVarPropertyConfigService _ipLCVarPropertyConfigService;

        private ObservableCollection<PLCVarPropertyConfig> _ItemList;
        /// <summary>
        /// 
        /// </summary>
        public ObservableCollection<PLCVarPropertyConfig> ItemList
        {
            get { return _ItemList; }
            set
            {

                SetProperty(ref _ItemList, value);
            }
        }


        private PLCVarPropertyConfig _SelectModel;
        /// <summary>
        /// 选择的Model
        /// </summary>
        public PLCVarPropertyConfig SelectModel
        {
            get { return _SelectModel; }
            set
            {
                SetProperty(ref _SelectModel, value);
                if (value != null)
                {

                    SelectModelChange();

                }
            }
        }


        private void SelectModelChange()
        {
            var datatype = SelectModel.VarTypeName;
            if (datatype != null)
            {
                var index = this.DataTypes.IndexOf(datatype);

                this.DataTypeIndex = index;

            }
            this.VarAddress = SelectModel.VarAddress;
            this.VarName = SelectModel.VarName;
            this.VarDescription=SelectModel.VarDescription;
          //  this.IsRuleVar = SelectModel.IsRuleVar;
        }

        private ObservableCollection<string> _DataTypes;

        /// <summary>
        /// 数据类型集合
        /// </summary>
        public ObservableCollection<string> DataTypes
        {
            get { return _DataTypes; }
            set
            {
                SetProperty(ref _DataTypes, value);

            }
        }

        /// <summary>
        /// 加载数据类型
        /// </summary>
        private void LoadDataTypes()
        {

            DataTypes = new ObservableCollection<string>(DataTypeEnumExtensions.GetAllCollectionTypeDescriptions());

        }
        /// <summary>
        /// 加载数据库信息
        /// </summary>
        private void LoadItemList()
        {
            if (ItemList!=null)
            {
                ItemList.Clear();
            }
          
            if (eventModel == null) 
            {
                Message = "未选择部件";
                return;
            }
            var data = _ipLCVarPropertyConfigService.GetData().Where(e=>e.DeviceID== eventModel.DeviceModel.ID&&e.ComponentID==eventModel.DeviceComponent.ID).ToList();
            if (data.Count==0) 
            {
                Message = "未找到该部件下的任何变量";
                return;
            }
            this.ItemList = new ObservableCollection<PLCVarPropertyConfig>(data);
            SelectModel = new PLCVarPropertyConfig();
            

        }
        private string _SelectDataType;

        public string SelectDataType
        {
            get { return _SelectDataType; }
            set
            {

                SetProperty(ref _SelectDataType, value);
                if (value != null)
                {
                    _SelectDataTypeEnum = DataTypeEnumExtensions.GetCollectionTypeByDescription(value);
                }
            }
        }

        private DataTypeEnum _SelectDataTypeEnum { get; set; }


        private int _DataTypeIndex;

        public int DataTypeIndex
        {
            get { return _DataTypeIndex; }
            set
            {
                SetProperty(ref _DataTypeIndex, value);

            }
        }



        private void AddData()
        {

            if (SelectModel==null) {

                SelectModel = new PLCVarPropertyConfig();
            }
            if ( string.IsNullOrEmpty(VarName))
            {
                Message = "变量名称为空 请检查";
                return;
            }

            var varname = VarName;


            var data = this.ItemList?.Where(e => e.VarName == varname&&e.DeviceID==eventModel.DeviceModel.ID&&e.ComponentID==eventModel.DeviceComponent.ID).ToList();
            if (data != null&&data.Count>0) 
            {
                Message = "变量名称重复请检查";
                return;
            
            }

            PLCVarPropertyConfig model = new PLCVarPropertyConfig();
            model.ID=Guid.NewGuid();
            model.VarName =this.VarName;
            model.DeviceID=eventModel.DeviceModel.ID;
            model.ComponentID = eventModel.DeviceComponent.ID;
            model.VarAddress = this.VarAddress;
            model.VarType = _SelectDataTypeEnum;
             model.VarDescription=this.VarDescription;
         //   model.IsRuleVar = this.IsRuleVar;
            var res=  _ipLCVarPropertyConfigService.AddData(model);
            if (res > 0)
            {
                Message = "添加成功";
                LoadItemList();
            }
            else { Message = "添加失败"; }
        }


        private void UpdateData() 
        {

            if (string.IsNullOrEmpty(VarName)) 
            {
                Message = "变量名称不能为空";
                return;         
            }

           

            this.SelectModel.VarType = _SelectDataTypeEnum;
            this.SelectModel.VarAddress = this.VarAddress;
            this.SelectModel.VarName = this.VarName;
            this.SelectModel.VarDescription = this.VarDescription;
          //  this.SelectModel.IsRuleVar = this.IsRuleVar;

            var res= _ipLCVarPropertyConfigService.UpdateData(this.SelectModel);

            if (res > 0)
            {
                Message = "更新成功";

            }
            else { Message = "更新失败"; }

            LoadItemList();

        }
        public DelegateCommand AddCommand { get; }

        public DelegateCommand UpdateCommand { get; }



        private string _Message;

        public string Message
        {
            get { return _Message; }
            set
            {

                SetProperty(ref _Message, value);

            }
        }


        private string _VarName;

        public string VarName
        {
            get { return _VarName; }
            set {
                SetProperty(ref _VarName,value);
                }
        }


        private string _VarAddress;

        public string VarAddress
        {
            get { return _VarAddress; }
            set {
                SetProperty( ref _VarAddress, value);
                
                 }
        }


        private string _VarDescription;

        public string VarDescription
        {
            get { return _VarDescription; }
            set {

                SetProperty(ref _VarDescription, value);
                }
        }


        //private bool _IsRuleVar;

        //public bool IsRuleVar
        //{
        //    get { return _IsRuleVar; }
        //    set {
        //        SetProperty(ref _IsRuleVar, value);
                
        //        }
        //}


    }
}
